<?php
namespace app\index\controller;
use app\index\model\Ticketlogs; 
/**
 * 支付接口
 * @author 
 * @version 2017-02-09 17:00:00
 */
class PaymentController extends BaseController {
	//购票
	public function buy_mticket(){
	header('Access-Control-Allow-Origin:*');
		if (request()->isAjax()) {
			$ticket_config = db('system')->field('value')->where('name', 'ticket_config')->find();
			$ticket_config = unserialize($ticket_config['value']);
			$nums=input("nums");
			$price=$ticket_config['ticket_price'];
			$sn=order_create_no();
			$this->add_ticket($sn,$price,$nums);
			import("payment.wxpay.WxPayApi");
			import("payment.wxpay.JsApiPay");
			$tools = new \JsApiPay();
			$input = new \WxPayUnifiedOrder();
			$input->SetBody('矿博会电子票');
			$input->SetAttach('矿博会电子票');
			$input->SetOut_trade_no($sn);
			$input->SetTotal_fee($price*$nums);
			$input->SetTime_start(date("YmdHis"));
			$input->SetTime_expire(date("YmdHis", time() + 600));
			// 回调通知处理
			$input->SetNotify_url(config('DOMAIN').'index/payment/wechat_notify');
			$input->SetTrade_type("JSAPI");
			$input->SetOpenid(session("openid"));
		
			$order = \WxPayApi::unifiedOrder($input);
			$jsApiParameters = $tools->GetJsApiParameters($order);
			echo $jsApiParameters;
			exit;
		} else {
			return $this->fetch();
		}
	}
	private function add_ticket($sn,$price,$nums){
		$user_id=session('user_info')['id'];	
		$list=array();
		for($i=0;$i<$nums;$i++)	{
			$data['nums']=1;
			$data['sn']=$sn;
			$data['user_id'] = $user_id;
			$data['add_time'] = now_time();
			$data['balance'] = $price;
			$data['pay_balance'] = $price;
			$list[]=$data;	
		}
		$member=new Ticketlogs();
		$result =$member->saveAll($list);
		if(!$result){
			echo "支付失败";
			exit;
		}
		
	}
	/**
	 * Post方法
	 * $body     商品描述
	 * $attach   附加数据，在查询API和支付通知中原样返回，可作为自定义参数使用。
	 * $order_no 订单号
	 * $total_fee 价格
	 */
	public function wechat() {
		header('Access-Control-Allow-Origin:*');
		if (request()->isAjax()) {
			import("payment.wxpay.WxPayApi");
			import("payment.wxpay.JsApiPay");
			//①、获取用户openid
			$tools = new \JsApiPay();
			//$openId = $tools->GetOpenid();
			//②、统一下单
			$input = new \WxPayUnifiedOrder();
			// 商品描述
			$input->SetBody('测试');
			// 商品补充说明
			$input->SetAttach('测试');
			// 商户订单号32位以下
			$input->SetOut_trade_no('20150826123456');
			// 价格，单位分
			$input->SetTotal_fee(1);
			$input->SetTime_start(date("YmdHis"));
			$input->SetTime_expire(date("YmdHis", time() + 600));
			// 回调通知处理
			$input->SetNotify_url(config('DOMAIN').'index/payment/wechat_notify');
			$input->SetTrade_type("JSAPI");
			$input->SetOpenid(session("openid"));
		
			$order = \WxPayApi::unifiedOrder($input);
			$jsApiParameters = $tools->GetJsApiParameters($order);
			echo $jsApiParameters;
			exit;
		} else {
			return $this->fetch();
		}
	}

	public function wechat_notify() {
		import("payment.wxpay.wechat_callback");
		//获取支付回调结果。
		$notify = new \wechat_callback();
		$notify->Handle(false);
		$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
		$data = $notify->FromXml($xml);
		//处理回调
		if($data['return_code'] == "SUCCESS"){
			//查找当前支付日志状态
			$sn=$data['out_trade_no'];
			file_put_contents("log.txt", now_time().$sn.PHP_EOL, FILE_APPEND);
			$ticket=db("ticketlogs")->where("sn",$sn)->find();
			//判断当前是否支付完成
			if($ticket['status'] != 2){
				db("ticketlogs")->where("sn",$sn)->update(["status"=>2,"pay_time"=>now_time()]);
			}else{
				$rest = $notify->ToXml();
				echo $rest;
			}
		}	
	}

}
